#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>

using std::cin;
using std::cout;
using std::priority_queue;
using std::vector;

#define lld long long
#define MAXN 1009
#define INF 1e17
struct edge {
  int u;
  lld dis;
};
bool operator<(edge a, edge b) { return a.dis > b.dis; }
void sol() {
  int n, m, x;
  cin >> n >> m >> x;
  vector<vector<edge>> g1(n + 1);
  vector<vector<edge>> g2(n + 1);
  lld dist1[MAXN], dist2[MAXN];
  bool vis1[MAXN], vis2[MAXN];

  std::fill(dist1, dist1 + 1 + n, INF);
  std::fill(dist2, dist2 + 1 + n, INF);

  std::memset(vis1, 0, sizeof(vis1));
  std::memset(vis2, 0, sizeof(vis2));

  priority_queue<edge> pq1;
  priority_queue<edge> pq2;
  for (int i = 1; i < m + 1; ++i) {
    int u, v, val;
    cin >> u >> v >> val;
    g1[u].push_back({v, val});
    g2[v].push_back({u, val});
  }

  // run g1
  dist1[x] = 0;
  pq1.push({x, 0});

  while (!pq1.empty()) {
    int u = pq1.top().u;
    lld dis = pq1.top().dis;
    pq1.pop();

    if (vis1[u])
      continue;

    for (auto x : g1[u]) {
      int v = x.u;
      lld val = x.dis;
      if (dist1[v] > dis + val) {
        dist1[v] = dis + val;
        pq1.push({v, dist1[v]});
      }
    }
  }

  // run g2
  dist2[x] = 0;
  pq2.push({x, 0});
  while (!pq2.empty()) {
    int u = pq2.top().u;
    lld dis = pq2.top().dis;
    pq2.pop();

    if (vis2[u])
      continue;

    for (auto e : g2[u]) {
      int v = e.u;
      lld val = e.dis;
      if (dist2[v] > dis + val) {
        dist2[v] = dis + val;
        pq2.push({v, dist2[v]});
      }
    }
  }
  // sum dist1+dist2
  lld ans = 0;
  for (int i = 1; i <= n; ++i) {
    if (dist1[i] == INF or dist2[i] == INF)
      continue;
    ans = std::max(ans, dist1[i] + dist2[i]);
  }
  cout << ans << "\n";
}
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int T = 1;
  cin >> T;
  while (T--) {
    sol();
  }
  return 0;
}